package com.atguigu.spzx.manager.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.atguigu.spyx.model.common.ResultCodeEnum;
import com.atguigu.spyx.model.common.com.atguigu.spyx.model.entity.product.Category;
import com.atguigu.spyx.model.dto.system.CategoryExcelDto;
import com.atguigu.spyx.model.exception.SpzxException;
import com.atguigu.spzx.manager.listtener.CategoryExcelDtoListener;
import com.atguigu.spzx.manager.mapper.CategoryMapper;
import com.atguigu.spzx.manager.service.CategoryService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author LT
 * @create 2023-11-07-20:49
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    CategoryMapper categoryMapper;

    @Resource
    CategoryExcelDtoListener categoryExcelDtoListener;

    @Override
    public List<Category> findByPid(Long pid) {

        List<Category> categoryList = categoryMapper.findByPid(pid);
        categoryList.forEach(category -> {
            category.setHasChildren(categoryMapper.hasChildren(category.getId()));
        });
        return  categoryList;
    }

    @CacheEvict(value = "product:cates" , key = "'all'")
    @Override
    public void importCates(MultipartFile excel) {

        try {
            EasyExcel.read(excel.getInputStream())
                    .head(CategoryExcelDto.class)
                    .registerReadListener(categoryExcelDtoListener)
                    //  .registerReadListener(new CategoryExcelDtoListener2(categoryMapper))
                    //                    .she
                    .doReadAll();//不指定sheet时 会读取所有的工作簿内容
        } catch (Exception e) {
            throw new SpzxException(ResultCodeEnum.FILE_UPLOAD_ERROR,e);
        }
    }

    @Override
    public void deleteById(Long id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public Category getByCategoryId(Long id) {
        return categoryMapper.getByCategoryId(id);
    }

    @Override
    public void updateById(Category category) {
        categoryMapper.updateById(category);
    }

    @Override
    public void exportCates(HttpServletResponse response)  {
        try {
        List<CategoryExcelDto> categoryExcelDtos = categoryMapper.findAll();

        String fileName = URLEncoder.encode("商品分类列表_"+System.currentTimeMillis()+ ExcelTypeEnum.XLSX.getValue());

        response.setHeader("content-disposition","attachment:file;"+
                "filename="+fileName);


            EasyExcel.write(response.getOutputStream())
                    .sheet("分类列表")
                    .head(CategoryExcelDto.class)
                    .doWrite(categoryExcelDtos);
        } catch (IOException e) {
            throw new SpzxException(ResultCodeEnum.FILE_EXPORT_ERROR,e);
        }

    }
}
